﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using YmtRpc.Web.Admin.Models;
using YmtRpc.Web.Admin.Service;
namespace YmtRpc.Web.Admin.Controllers
{
   [Authorize]
    public class AccountController : Controller
    {
        public AccountController()
        {
        }

        //
        // GET: /Account/Login
        [AllowAnonymous]
        public ActionResult Login(string returnUrl)
        {
            ViewBag.ReturnUrl = returnUrl;
            return View();
        }

        //
        // POST: /Account/Login
        [HttpPost]
        [AllowAnonymous]
        //[ValidateAntiForgeryToken]
        public ActionResult Login(LoginViewModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                try
                {
                   AuthenticationService service = new AuthenticationService();
                    bool flag = service.Login(model.LoginId, model.Password, null);
                    if (string.IsNullOrEmpty(returnUrl))
                    {
                        returnUrl = "/Home";
                    }
                    //return Redirect(returnUrl);
                    if (flag)
                        return Redirect(returnUrl);
                    else
                    {
                        ViewBag.ErrorMessage = "验证失败！";
                        return View(model);
                    }
                }
                catch (Exception)
                {
                    ViewBag.ErrorMessage = "验证失败！";
                    return View(model);

                }
            }
            else
            {
                return View(model);
            }

        }



        public ActionResult Logoff()
        {
            AuthenticationService service = new AuthenticationService();
            service.Logoff();
            return Redirect("/Home");
        }
    }
}